<?php
namespace cubox\provider\webpack;

use cubox\Webpack;
use cubox\provider\template\Asset;


class Image extends File{
    
    public function pack(){
        $ext = pathInfo($this->file,PATHINFO_EXTENSION);

        $content =  '';
        $funName = $this->getFunctionName($this->file);
         
        $fileContent = Asset::url($this->webpackUrl,$this->file);
         
        if (isset($this->param['name']) || $this->useExport){
             $content = "\nfunction ".$funName."(){";
             $content .= "\nreturn ";
             $content .= '"'.$fileContent.'"';
             $content .= "\n}";
        }
        
        Webpack::addjs($this->name,$content);
    }


    protected function getContent(){

        if (isset(self::$fileContent[$this->file])){
            return self::$fileContent[$this->file];
        }

        $content = '';
        if (file_exists($this->file)){
            $content = base64_encode( file_get_contents($this->file));
        }

        self::$fileContent[$this->file] = $content;
        
        return $content;
    }
}